package com.wd.mylibrary_live.view.activity

import android.media.MediaPlayer
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.ayvytr.adapter.smart
import com.wd.mylibrary_base.base.BaseActivity
import com.wd.mylibrary_base.bean.MusicBean
import com.wd.mylibrary_base.utils.MyApp
import com.wd.mylibrary_live.R
import com.wd.mylibrary_live.adapter.MusicAdapter
import com.wd.mylibrary_live.databinding.ActivityMusicBinding
import com.wd.mylibrary_live.viewmodel.MusicViewModel

class MusicActivity : BaseActivity<MusicViewModel,ActivityMusicBinding>(R.layout.activity_music) {
    val musicList:ArrayList<MusicBean.Result> = ArrayList()
    lateinit var adapter:MusicAdapter
    var mediaPlayer:MediaPlayer= MediaPlayer()

    override fun initView() {
        var userId = MyApp.sp.getInt("userId", 0)
        var sessionId = MyApp.sp.getString("sessionId","").toString()

        viewModel.getMusicData(1,10)

    //设置适配器
        adapter= MusicAdapter(this,musicList,mediaPlayer)
        dataBinding.rv.adapter=adapter
        dataBinding.rv.layoutManager=LinearLayoutManager(this)
        dataBinding.rv.addItemDecoration(DividerItemDecoration(this,LinearLayoutManager(this).orientation))

        //返回
        dataBinding.back.setOnClickListener {
            finish()
        }

        //调用适配器选择点击回调销毁页面并存储歌曲url和名称
        adapter.setOnClickListener(object :MusicAdapter.OnClickListener{
            override fun music(id: Int, list: ArrayList<MusicBean.Result>) {
                MyApp.sp.edit().putString("musicLinks",list[id].musicLinks)
                    .putString("musicName",list[id].name)
                    .commit()
                finish()
            }
        })
    }

    override fun dataObserve() {
        viewModel.musicData.observe(this,{
            if(it.result!=null){
                musicList.addAll(it.result)
                adapter.notifyDataSetChanged()
            }
        })

    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayer.release()
    }
}